package com.ruoyi.service.card.impl;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * TODO
 *
 * @author chenchuan2
 * @date 2024-12-18 22:28:05
 */
public class AESCrypto {
    // 与JS代码保持一致的密钥和IV
    private static final String KEY = "tomSun28HaHaHaHa";
    private static final String IV = "tomSun28HaHaHaHa";

    /**
     * AES解密
     * @param encryptedData Base64编码的加密数据
     * @return 解密后的字符串
     */
    public static String aesDecrypt(String encryptedData) throws Exception {
        try {
            byte[] encrypted = Base64.getDecoder().decode(encryptedData);

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));

            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] decrypted = cipher.doFinal(encrypted);

            return new String(decrypted, StandardCharsets.UTF_8);

        } catch (Exception e) {
            throw new Exception("Decryption failed: " + e.getMessage());
        }
    }

    /**
     * AES加密
     * @param data 要加密的字符串
     * @return Base64编码的加密字符串
     */
    public static String aesEncrypt(String data) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));

            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));

            return Base64.getEncoder().encodeToString(encrypted);

        } catch (Exception e) {
            throw new Exception("Encryption failed: " + e.getMessage());
        }
    }

    /**
     * 使用示例
     */
    public static void main(String[] args) {
        try {
            // 测试加密
//            String originalText = "{\"number\":\"123456\",\"barCode\":\"789\",\"qrCode\":\"xyz\"}";
//            String encrypted = aesEncrypt(originalText);
//            System.out.println("Encrypted: " + encrypted);

            // 测试解密
            String decrypted = aesDecrypt("h64pqYW89ppTN/UdaHRmPKq5SaOVSIGXlVKGcPhLOR5RY1O72sbE1JF1WOKZCI7S0lMeJrYr/hcPJRokS5xV5KECWXFRHCOjwVVu2QFWBbngT8BlpkQuRi2mdVYWIqpfE5RpgNb6TiUyZJO0/xVzs+HfOlQSCHXrsTDQnDpH9Fdmi+dBltotQlFKdQrzhwnRKCgYTvj0yDMnoREUFUh0cwpTkydSJhpjOf6CQGrLf45Z7bx87j7rCVAZXkTbXzqnLS2qyHUOvDbD2Nfo41KlXFBL7i6li2hK++5wKdSgDRWUY6FRjECjabcT8xWm8oE/qAldnXKOkEVQANVe9YXKmw/BPLTxBkicFagq0tGqLoVJkTN0pv+O5CX2jslGDPHbeNqcwY6eCHPrduvdkJT+CARGxsXK3KE+zO/Jh1IEM674Nb0ojMGIt6khqrc0qsddFm7AlhXQoUgO6DK/+9VxQEaXF+wf4oldjPKrgCdqwCtFkg5WP/5zK3dJ3M9s0YThuXuJVq3yY6hbtL5vUUWfl+2euPqny1+WiVpZxQ8zaHbzb6RQF6tL914W0iSUEs8R2mHnXk99qRedlyyCV5CMLzM9Wm1GwahL/IGI1hSJABm66fiezZs8DqOJxyPn/hBOqfNcvoUAXi7OmFzLP8IFYNXhXCcg+TcP8bKxb974gUElj6+CNruCIDAbN7JuGZpfC8e6WLkEfC3e+bvjB9PCYZyyh0d9QGtZMZjqygKmtofqXa3mudpzl6keKHeuW8ozkgovExiP3Vn2WE+4W6svRStRQhs0OmSXK/3EeLKWYglF/k+rQ+IuvtIKMJTLOkHeixHJG3wrrfuteftmMBI2wAWWBgXuUhJqIXJs60RedBiS5am7qtcduDHs4j85gpNhXBwtXLNnuWukIM6a/siaxRGWeMfcxr6za9pxSLcwrQrXq/1fWQh+ISSWa1b1QKmlxRSqnOf95LFKTcFYdIFHabPWvWbwGIdF5HngtipspIyzXZ6U6EkhlB7whuCmirhrO3Q8yoVlwR/e82OzfmgcQIVmIJavvZtlcZs2SmxyWuJNxGLMC/N9SaxgV7+JtM+XqPX4ExD3Y0CY5l+CfPRDusSA9Yp3zghCInunzqxhUgk=");
            System.out.println("Decrypted: " + decrypted);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
